#include "AdInterstitialForm.h"
#include <QTimer>
#include <QVBoxLayout>

AdInterstitialForm::AdInterstitialForm()
{
    control = new AdViewControl(this);

    autoCloseInterstitialTime = 0;
    closeButtonShowTime = 0;
    layout = new QVBoxLayout;
    layout->addWidget(control);
    this->setLayout(layout);
}

void AdInterstitialForm::showEvent( QShowEvent * event )
{
    if(autoCloseInterstitialTime > 0 ){
        closeTimer = new QTimer(this);
        closeTimer->stop();
        closeTimer->start(autoCloseInterstitialTime*1000);
        connect(closeTimer, SIGNAL(timeout()), this, SLOT(closeTimerTimeOut()));
    }

    if ( (closeButtonShowTime > 0) && (closeButton != NULL) )
    {
        showButtonTimer = new QTimer(this);
        showButtonTimer->stop();
        showButtonTimer->start(closeButtonShowTime*1000);
        closeButton->setVisible(false);
        connect(showButtonTimer, SIGNAL(timeout()), this, SLOT(showButtonTimerTimeOut()));
        connect(closeButton, SIGNAL(clicked()), this, SLOT(closeTimerTimeOut()));
    }


    control->Update();
}

void AdInterstitialForm::showButtonTimerTimeOut(){
    layout->addWidget(closeButton);
    closeButton->setVisible(true);
}

void AdInterstitialForm::closeTimerTimeOut(){
     this->done(QDialog::Rejected);
}


int AdInterstitialForm::getAutoCloseInterstitialTime()
{
    return autoCloseInterstitialTime;
}

void AdInterstitialForm::setAutoCloseInterstitialTime(int closeButtonTime)
{
    autoCloseInterstitialTime = closeButtonTime;
}


int AdInterstitialForm::getCloseButtonShowTime()
{
    return closeButtonShowTime;
}

void AdInterstitialForm::setCloseButtonShowTime(int closeButtonTime)
{
    closeButtonShowTime = closeButtonTime;
}


AdViewControl* AdInterstitialForm::getControl()
{
    return control;
}

void AdInterstitialForm::setCloseButton(QPushButton* button)
{
    closeButton = button;
}


QPushButton* AdInterstitialForm::getCloseButton()
{
    return closeButton;
}
